<%@ page import="entity.Admin" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
  String path = request.getContextPath();
  Admin admin = (Admin) session.getAttribute("admin");
  List<String> privs = (List<String>) session.getAttribute("privs");
%>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>kms管理系统</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache, must-revalidate">

<link rel="stylesheet" type="text/css" href="<%=path %>/css/bootstrap-table.min.css">
<link rel="stylesheet" type="text/css" href="<%=path %>/css/normalize.css">
<link rel="stylesheet" type="text/css" href="<%=path %>/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="<%=path %>/bootstrap-datetimepicker/bootstrap-datetimepicker.min.css">
<link rel="stylesheet" type="text/css" href="<%=path %>/css/bootstrap-switch.min.css">
<link rel="stylesheet" type="text/css" href="<%=path %>/css/main.css">
<link rel="stylesheet" type="text/css" href="<%=path %>/css/sweetalert.css">

<style>
body { background: linear-gradient(90deg, #fff, #ffffef 50%, #fff); }
article { background: #fff; }
.fz-brand { max-width: 180px; position: absolute; clip: rect(25px 180px 80px 0); top: -30px; left: 8px; }
</style>
</head>
<body>

<header></header>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="javascript:window.location.reload();">
        <img class="fz-brand" src="<%=path %>/images/fzu_logo2.jpg" alt=""/>
      </a>
    </div>
    <div class="navbar-text navbar-right">
      <%=admin.getAdminName() %>
      <%=admin.getIsSuper() %>管理员&nbsp;
      隶属部门：<span id="curDept"><%=admin.getDeptName() %></span>&nbsp;
      <a href="#myModal" data-toggle="modal" id="edit_password" class="navbar-link">修改密码</a>
      <a href="javascript:;" class="navbar-link" id="log_out">退出</a>&nbsp;&nbsp;
    </div>
  </div>

  <ul class="nav nav-pills" id="menus">
    <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">基础管理 <span class="caret"></span></a>
      <ul class="dropdown-menu" role="menu">
        <li><a href="javascript:;" id="admins_basic">管理员管理</a></li>
        <li><a href="javascript:;" id="depts_basic">部门管理</a></li>
        <li><a href="javascript:;" id="users_basic">用户管理</a></li>
        <li><a href="javascript:;" id="apps_basic">应用管理</a></li>
      </ul>
    </li>

    <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">激活管理 <span class="caret"></span></a>
      <ul class="dropdown-menu" role="menu">
        <li><a href="javascript:;" id="softs_active">软件管理</a></li>
        <%--<li><a href="javascript:;" id="keys_active" class="hidden">密钥管理</a></li>--%>
        <li><a href="javascript:;" id="users_soft_active">用户激活情况</a></li>
        <li><a href="javascript:;" id="users_approve_active">用户许可审批</a></li>
        <li><a href="javascript:;" id="depts_assign_active">部门许可分配</a></li>
      </ul>
    </li>

    <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">数据统计 <span class="caret"></span></a>
      <ul class="dropdown-menu" role="menu">
        <li><a href="javascript:;" id="keys_data">密钥数量统计</a></li>
        <li><a href="javascript:;" id="signup_data">注册用户统计</a></li>
        <li><a href="javascript:;" id="active_data">软件激活统计</a></li>
        <li><a href="javascript:;" id="assign_data">软件许可统计</a></li>
        <li><a href="javascript:;" id="err_data">激活错误统计</a></li>
      </ul>
    </li>
  </ul>
</nav>

<article>
  <div id="content"></div>
  <nav class="nav nav-page hidden">
    <ul class="pagination hidden" id="page_nav" style="margin-left: 15px;">
    </ul>
  </nav>
</article>

<!-- 模态框 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
          &times;
        </button>
        <h4 class="modal-title" id="modalLabel">
          <span class="text-primary"></span>
        </h4>
      </div>
      <div class="modal-body">
        <div id="modalBody"></div>
      </div>
      <div class="modal-footer">
        <button class="btn btn-default" data-dismiss="modal">关闭</button>
        <button class="btn btn-primary" id="modalAct"></button>
      </div>
    </div>
  </div>
</div>

<footer></footer>

<script type="text/javascript" src="<%=path %>/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="<%=path %>/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="<%=path %>/js/bootstrap-table.min.js"></script>
<script type="text/javascript" src="<%=path %>/js/Blob.js"></script>
<script type="text/javascript" src="<%=path %>/js/FileSaver.js"></script>
<script type="text/javascript" src="<%=path %>/js/tableExport.js"></script>
<script type="text/javascript" src="<%=path %>/js/require.js"></script>

<script>
require.config({
  baseUrl: '<%=path %>/js',
  paths: {
    jquery: 'jquery-1.9.1.min',
    commmon: 'common',
    md5: 'md5',
    sweetalert: 'sweetalert.min'
  }
});

require(['jquery', 'common', 'md5', 'sweetalert'], function ($, common, md5, swal) {
//	console.log("主页载入");

var workspace = function () {

  var domKey;
  var menusDom;
  var menuOpts;

  var privs;

  var init = function () {
    domKey = common.commData.domKey;

    // 权限数组
    privs = "<%=privs %>".substring(1, "<%=privs %>".length - 1).split(", ");

    menusDom = {
      // 基础管理
      adminsBasic: $("#admins_basic"),
      deptsBasic: $("#depts_basic"),
      usersBasic: $("#users_basic"),
      appsBasic: $("#apps_basic"),

      // 激活管理
      softsActive: $("#softs_active"),
      usersSoftActive: $("#users_soft_active"),
      usersApproveActive: $("#users_approve_active"),
      deptsAssignActive: $("#depts_assign_active"),

      // 数据统计
      keysData: $("#keys_data"),
      signupData: $("#signup_data"),
      assignData: $("#assign_data"),
      activeData: $("#active_data"),
      errData: $("#err_data")
    }

    _initMenusBind();
    _privsControl();
    _logOut();
    _search();
    _editPassword();
  }

  // 菜单页面绑定
  var _initMenusBind = function () {
    menuOpts = [
      // 基础管理
      {menuDom: menusDom.adminsBasic, url: "/views/admin/admins_list.jsp", name: "管理员管理"},
      {menuDom: menusDom.deptsBasic, url: "/views/department/depts_list.jsp", name: "部门管理"},
      {menuDom: menusDom.usersBasic, url: "/views/user/users_list.jsp", name: "用户管理"},
      {menuDom: menusDom.appsBasic, url: "/views/download/downloads_list.jsp", name: "应用管理"},

      // 激活管理
      {menuDom: menusDom.softsActive, url: "/views/soft/softs_list_2.jsp", name: "软件管理"},
      {menuDom: menusDom.usersSoftActive, url: "/views/user/users_soft_active.jsp", name: "用户激活情况"},
      {menuDom: menusDom.usersApproveActive, url: "/views/user/users_approve.jsp", name: "用户许可审批"},
      {menuDom: menusDom.deptsAssignActive, url: "/views/department/depts_assign.jsp", name: "部门许可分配"},

      // 数据统计
      {menuDom: menusDom.keysData, url: "/views/data/keys_data.jsp", name: "密钥数量统计"},
      {menuDom: menusDom.signupData, url: "/views/data/signup_data.jsp", name: "注册用户统计"},
      {menuDom: menusDom.assignData, url: "/views/data/assign_data.jsp", name: "软件许可统计"},
      {menuDom: menusDom.activeData, url: "/views/data/active_data.jsp", name: "软件激活统计"},
      {menuDom: menusDom.errData, url: "/views/data/err_data.jsp", name: "激活错误统计"}
    ];

    menuOpts.forEach(function (menuOpt) {
      common.menusBind(menuOpt);
    });
  }

  // 登出清除session的admin
  var _logOut = function () {
    $("#log_out").click(function () {
      common.cleanSession({"name": "admin"});
    });
  }

  // 管理员权限控制
  var _privsControl = function () {
    var isSuper = "<%=admin.getIsSuper() %>";

    if ("部门" === "<%=admin.getIsSuper() %>") {
      menusDom.deptsBasic.parent().hide();// 隐藏部门管理
      menusDom.adminsBasic.parent().hide();// 隐藏管理员管理
      menusDom.deptsAssignActive.parent().hide();// 隐藏部门许可分配
      menusDom.softsActive.parent().hide();// 隐藏软件管理
    }

    // 隐藏没有该功能权限的菜单
    var menus = menuOpts;
    var f;
    menus.forEach(function (menu) {
      f = true;
      for (var i = 0; i < privs.length; i++) {
        if (privs[i] === menu.name) {
          privs.splice(i, 1);// 删除已隐藏菜单，提高性能
          f = false;
          break;
        }
        f = true;
      }
      if (f) {
        menu.menuDom.hide();
      }
    });
  }

  // 修改当前管理员密码
  var _editPassword = function () {
    var modalBodyTemplate = "<form class='form-horizontal'>" +
      "<div class='form-group'>" +
      "<label for='oldPassword' class='col-sm-4 control-label'>原始密码</label>" +
      "<div class='col-sm-8'>" +
      "<input type='password' class='form-control' name='oldPassword' id='oldPassword' placeholder='原始密码' autocomplete='off' />" +
      "</div></div>" +
      "<div class='form-group'>" +
      "<label for='newPassword' class='col-sm-4 control-label'>新密码</label>" +
      "<div class='col-sm-8'>" +
      "<input type='password' class='form-control' name='newPassword' id='newPassword' placeholder='新密码' autocomplete='off' />" +
      "</div></div>" +
      "<div class='form-group'>" +
      "<label for='newPassword2' class='col-sm-4 control-label'>确认密码</label>" +
      "<div class='col-sm-8'>" +
      "<input type='password' class='form-control' name='newPassword2' id='newPassword2' placeholder='确认密码' autocomplete='off' />" +
      "</div></div></form>";

    $("#edit_password").click(function () {
      common.cleanModal();
      domKey.modal.find(".modal-dialog").addClass("modal-sm");
      domKey.modalLabel.html("修改密码");
      domKey.modalBody.html(modalBodyTemplate);

      domKey.modalAct.html("确认")
      .on("click", function () {
        common.disable(domKey.modalAct);// 提交请求则禁掉按钮

        var data = {
          "table": "admins",
          "name": "<%=admin.getAdminName() %>",
          "oldPassword": md5($("#oldPassword").val()),
          "newPassword": md5($("#newPassword").val()),
          "newPassword2": md5($("#newPassword2").val())
        }
        if ($("#oldPassword").val().length === 0 || $("#newPassword").val().length === 0 || $("#newPassword2").val().length === 0) {
          alert("请填写所需信息");
          common.enable(domKey.modalAct);
        } else {
          // 旧密码验证
          var step1 = data["oldPassword"] == "<%=admin.getPassword() %>";
          if (step1) {
            // 重复密码验证
            var step2 = data["newPassword"] == data["newPassword2"];
            if (step2) {
              var opt = {
                data: data,
                fun: function () {
                  alert("修改成功，请重新登录");
                  $("#log_out").click();
                }
              }
              common.editPassword(opt);
            } else {
              swal("sorry", "两次密码不一致", "error");
              common.enable(domKey.modalAct);
            }
          } else {
            swal("sorry", "原始密码错误", "error");
            common.enable(domKey.modalAct);
          }
        }
      });
    });
  }

  // 回车点击搜索
  function _search(e) {
    $(document).keydown(function (e) {
      if (!e)
        e = e || window.event;
      if ((e.keyCode || e.which) === 13) {
        $("#doSearch").click();
      }
    });
  }

  return {
    init: init
  }
}();

$(function () {
  // 公共区常用常量
  var args = {
    contextPath: "<%=path %>",
    domKey: {
      content: $("#content"),
      modalLabel: $("#modalLabel span"),
      modalBody: $("#modalBody"),
      modalAct: $("#modalAct"),
      modal: $("#myModal")
    }
  }

  common.initData(args);
  workspace.init();

  // 日期格式化
  Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
      "M+": this.getMonth() + 1, //月份
      "d+": this.getDate(), //日
      "h+": this.getHours(), //小时
      "m+": this.getMinutes(), //分
      "s+": this.getSeconds(), //秒
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度
      "S": this.getMilliseconds() //毫秒
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  }
});

});
</script>
</body>
</html>